package actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.Contact;
import org.brickred.socialauth.Profile;
import org.brickred.socialauth.SocialAuthManager;
import org.brickred.socialauth.util.SocialAuthUtil;

import de.deltatree.social.web.filter.api.SASFHelper;
import de.deltatree.social.web.filter.api.SASFStaticHelper;

public class SocialAuthenticationSuccessAction {
	
	Profile profile;
	List<Contact> contactsList;
	
	
	public String socialSuccessViaFilter() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		SASFHelper helper = SASFStaticHelper.getHelper(request);
		profile = helper.getProfile();

		contactsList = helper.getContactList();
		
		System.out.println("List Contact Size " + contactsList.size());
		
		for (Contact contact : contactsList) {
			System.out.println("DisplayName " + contact.getDisplayName());
			System.out.println("Email " + contact.getEmail());

		}

		request.setAttribute("profile", profile);
		request.setAttribute("contactList", contactsList);

		return "success";
	}

	public String socialSuccessViaApi() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		HttpSession session = request.getSession();
		
		SocialAuthManager manager = (SocialAuthManager) session.getAttribute("manager");		
		
		List<Contact> contactsList = new ArrayList<Contact>();
		
      	Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
      	
		AuthProvider provider = manager.connect(paramsMap);

		profile = provider.getUserProfile();
		
		contactsList = provider.getContactList();
		
		for (Contact contact : contactsList) {
			System.out.println("getDisplayName " + contact.getDisplayName());
		}		

		return "success";
	}

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public List<Contact> getContactsList() {
		return contactsList;
	}

	public void setContactsList(List<Contact> contactsList) {
		this.contactsList = contactsList;
	}	
	
	
	
}
